package com.realgodo.site.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.realgodo.site.common.Constant;
import com.realgodo.util.FileUpload;
import com.realgodo.util.OperateImage;

@Controller("ajaxPicuureCutAction")
@Scope("prototype")
public class CutController extends ActionSupport {

	private static final long serialVersionUID = 7399075529625187974L;

	/**
	 * 剪切左上点坐标 x
	 */
	private Double selectorX;
	/**
	 * 剪切左上点坐标 y
	 */
	private Double selectorY;

	/**
	 * 剪切点宽度
	 */
	private Double selectorW;
	/**
	 * 剪切点高度
	 */
	private Double selectorH;

	/**
	 * 剪切前图片地址
	 */
	private String imageSource;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

	private Date currentDate = Calendar.getInstance().getTime();
	/**
	 * 剪裁后实际地址
	 */
	private String sourcepath;

	public String cut() {
		if (imageSource != null && !imageSource.isEmpty()) {
			imageSource = imageSource.replace("meimei", "");
			imageSource = imageSource.replace("/", "\\");
			OperateImage o = new OperateImage(selectorX.intValue(), selectorY
					.intValue(), selectorW.intValue(), selectorH.intValue());
			imageSource = Struts2Utils.getRequest().getSession()
					.getServletContext().getRealPath(imageSource);

			// 目标文件

			String filename = FileUpload.getFileName(imageSource);
			String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
					+ "/" + filename;
			sourcepath = Struts2Utils.getRequest().getSession()
					.getServletContext().getRealPath(temp);
			/**
			 * 剪裁后网络地址
			 */
			String webPath = Constant.ROOT_PATH + temp;
			try {
				o.cut(imageSource, sourcepath);
			} catch (IOException e) {
				e.printStackTrace();
			}
			Struts2Utils.renderText(webPath);

		}
		return null;
	}

	/************** getter and setter ******/

	public String getImageSource() {
		return imageSource;
	}

	public void setImageSource(String imageSource) {
		this.imageSource = imageSource;
	}

	public String getSourcepath() {
		return sourcepath;
	}

	public void setSourcepath(String sourcepath) {
		this.sourcepath = sourcepath;
	}

	public Double getSelectorX() {
		return selectorX;
	}

	public void setSelectorX(Double selectorX) {
		this.selectorX = selectorX;
	}

	public Double getSelectorY() {
		return selectorY;
	}

	public void setSelectorY(Double selectorY) {
		this.selectorY = selectorY;
	}

	public Double getSelectorW() {
		return selectorW;
	}

	public void setSelectorW(Double selectorW) {
		this.selectorW = selectorW;
	}

	public Double getSelectorH() {
		return selectorH;
	}

	public void setSelectorH(Double selectorH) {
		this.selectorH = selectorH;
	}

}
